<?php

/**
 * 字典查询、添加、编辑
 * @version v1.0
 */


class pa_item {
	
	public function execute () {
		if (isset($_REQUEST['operFlg'])) {
			$operFlg = $_REQUEST['operFlg'];
			switch ( $operFlg ) {
				case 1: //字典表查询
					$this->select();
					break;
				case 2: //编辑
					$this->edit( $operFlg );
					break;
				case 3: //添加
					$this->add( $operFlg );
					break;
					
			}
		}
	}
	/**
	 * 查询字典信息
	 * @author raohenglei
	 * 
	 */
	private function select() {
		global $G_PGLOB;
		
		//实例化字典表操作公共类
		$itemHandler = new itemHandler( $G_PGLOB->DBCA );
		
		//获取分类ID
		if (!isset($_REQUEST['item']) || empty($_REQUEST['item']) || !is_numeric($_REQUEST['item'])) {
			$G_PGLOB->smarty->assign('error_msg', '参数错误，请联系系统管理员');
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			die();
		}
		$item_category_id = $_REQUEST['item'];
		
		try {
			//查询分类信息
			$category_array = $itemHandler->getItemCategory( $item_category_id );
			if (is_null($category_array))
				throw new Exception( '系统错误，请联系系统管理员' , -1000 );
				
			$category_array = $category_array[0];
			$G_PGLOB->smarty->assign_by_ref('category_array', $category_array);
			$G_PGLOB->smarty->assign_by_ref('item_category_id', $item_category_id);
			
			$G_PGLOB->smarty->display('item/itemList.tpl');
			
		} catch (Exception $e) {
			if ($e->getCode() == -1000) {
				$G_PGLOB->smarty->assign('error_msg', $e->getMessage());
			} else {
				error_log($e->getMessage());			
			}
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		}
	}
	
	/**
	 * 编辑和添加字典信息
	 *
	 */
	private function edit( $operFlg ) {
		global $G_PGLOB;
		
		//实例化字典表操作类
		$itemHandler = new itemHandler( $G_PGLOB->DBCA );
		
		//获取分类ID
		if (!isset($_REQUEST['item']) || !isset($_REQUEST['itemID']) || !is_numeric($_REQUEST['item']) || !is_numeric($_REQUEST['itemID'])) {
			$G_PGLOB->smarty->assign('error_msg', '参数错误，请联系系统管理员');
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		}
		
		$item_category_id = $_REQUEST['item'];
		$item_id = $_REQUEST['itemID'];
		
		try {
			
			//查询分类信息
			$category_array = $itemHandler->getItemCategory( $item_category_id );
			if (is_null($category_array))
				throw new Exception( '系统错误，请联系系统管理员' , -1000 );
			$category_array = $category_array[0];
			
			//查询字典信息
			$item_array = $itemHandler->getItemInfo( $item_id );
			if(!$item_array){							
				throw new Exception( $category_array->itemCategoryName . '信息没有找到，请联系系统管理员', -1000);
			}
			$item_array = $item_array[0];
			
			$G_PGLOB->smarty->assign_by_ref('item_array', $item_array);
			$G_PGLOB->smarty->assign_by_ref('category_array', $category_array);
			$G_PGLOB->smarty->assign('item_category_id',$item_category_id);
			$G_PGLOB->smarty->assign('operFlg',$operFlg);
						
			if ($category_array->item_tpl)
				$G_PGLOB->smarty->display($category_array->item_tpl);
			else
				$G_PGLOB->smarty->display('item/itemEdit.tpl');
			
		} catch ( Exception $e ) {
			
			if ($e->getCode() == -1000) {
				$G_PGLOB->smarty->assign('error_msg', $e->getMessage());
			} else {
				error_log($e->getMessage());			
			}
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			
		}
	}
	/**
	 * 添加字典信息
	 *
	 */
	private function add () {
		global $G_PGLOB;
		
		//实例化字典表操作类
		$itemHandler = new itemHandler( $G_PGLOB->DBCA );
		
		$item_category_id = $_REQUEST['category_id'];
		
		$operFlg = $_REQUEST['operFlg'];
		
		try {
			
			//查询分类信息
			$category_array = $itemHandler->getItemCategory( $item_category_id );
			if (is_null($category_array))
				throw new Exception( '系统错误，请联系系统管理员' , -1000 );
			$category_array = $category_array[0];
			
			$G_PGLOB->smarty->assign('category_array',$category_array);
			$G_PGLOB->smarty->assign('operFlg',$operFlg);
						
			$G_PGLOB->smarty->display('item/itemEdit.tpl');
			
		} catch ( Exception $e ) {
			
			if ($e->getCode() == -1000) {
				$G_PGLOB->smarty->assign('error_msg', $e->getMessage());
			} else {
				error_log($e->getMessage());			
			}
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			
		}
	}
	
}
